<template>
    <div id="tmpl">
        <!--图片分类-->
        <div id="cate">
            <ul v-bind="{style:'width:'+ulWidth+'px'}">
                <li @click="getimages(0)">全部</li>
                <li v-for="item in cates" @click="getimages(item.id)">{{item.title}}</li>
            </ul>
        </div>

        <!--图片列表-->
        <div id="imglist">
            <ul>
                <li v-for="item in list">
                    <router-link v-bind="{to:'/photo/photoinfo/'+item.id}"><img v-lazy="item.img_url">
                        <div id="desc">
                            <h5 v-text="item.title"></h5>
                            <p v-text="item.content"></p>
                        </div>
                    </router-link>
                </li>
            </ul>
        </div>
    </div>
</template>

<script>
    import common from '../../common/common.js';
    import {Toast} from 'mint-ui';

    export default {
        data() {
            return {
                ulWidth: 320,
                cates: [], //用来存储图片分类数据的数组
                list: [] //存放图片的数据
            }
        },
        created() {
            //获取图片分类数据
            this.getcates();
            //加载数据
            var all = 0;//获取所有数据
            this.getimages(all);
        },
        methods: {
            getimages(cateid) {
                cateid = cateid || 0;
                var url = common.apidomain + '/vue/getimage.php';

                this.$http.get(url).then(function (res) {
                    if (res.body.status != 0) {
                        Toast(body.message);//mint组件
                        return;
                    }

                    // 如果服务器返回的数据正常则赋值给list
                    this.list = res.body.message;
                })
            },
            getcates() {
                var url = common.apidomain + '/vue/getimgcategory.php';
                //调用$http的get方法获取数据
                this.$http.get(url).then(function (res) {
                    if (res.body.status != 0) {
                        Toast(body.message);//mint组件
                        return;
                    }

                    // 如果服务器返回的数据正常则赋值给list
                    this.cates = res.body.message;

                    //实现当前分类数据所在的url总宽度 = 分类个数*每个分类数据的宽度
                    var w = 62;
                    var count = res.body.message.length + 1;
                    this.ulWidth = w * count;
                })
            }
        }
    }
</script>

<style scoped>
    #cate {
        width: 375px;
        max-width: 375px;
        overflow-x: auto;
    }

    #cage ul {
        margin: 0px;
        padding-left: 10px;
    }

    #cate li {
        cursor: pointer;
        list-style: none;
        display: inline-block;
        color: #0094ff;
        font-size: 14px;
        padding-left: 6px;
    }

    /*图片列表样式*/
    #imglist {

    }

    #imglist ul {
        padding-left: 0px;
    }

    #imglist li {
        list-style: none;
        position: relative;
    }

    #imglist img {
        width: 100%;
        height: 300px
    }

    #desc {
        width: 100%;
        background-color: rgba(0, 0, 0, 0.2);
        position: absolute;
        bottom: 2px;
        left: 0px
    }

    #desc h5 {
        color: #ffffff;
        font-weight: bold;
    }

    #desc p {
        color: #fff;
    }

    image[lazy=loading] {
        width: 40px;
        height: 300px;
        margin: auto;
    }
</style>